package bTree.LeetCode;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 在每个树行中找最大值 {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer>res=new ArrayList<>();
        if (root==null){
            return res;
        }
        Queue<TreeNode>queue=new LinkedList<TreeNode>();
        queue.add(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            int max=Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode peek = queue.peek();
                if (peek.left!=null){
                    queue.add(peek.left);
                }
                if (peek.right!=null){
                    queue.add(peek.right);
                }
                int value=peek.val;
                max=Math.max(max,value);
                queue.poll();
            }
            res.add(max);
        }
        return res;
    }
}